package com.finance.cooperate.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName AmountExtractUtils
 * @Description TODO
 * @Author shenanxin
 * @Date 2023/10/18 16:56
 * @Modify ...
 */
public final class AmountExtractV1Utils {

    /**
     * Cashpaddie: 贷款额度为N20000。
     * NairaLoan: 下一次贷款可获得的金额为N15600。这条短信中并没有明确提到当前贷款的金额。
     * Debit: 贷款总还款金额为NGN6,866.67。
     * Super loan: 还款金额为32000.0，即N32000。
     * FIRSTNELL: 贷款金额为28140.00。
     * CayCredit: 贷款额度为70000 NGN，未明确提到当前欠款金额。
     * CreditLoans: 下一次贷款可获得的金额为N39400，这条短信中并没有明确提到当前贷款的金额。
     **/
    public static Double extractAmount(String smsText) {

        smsText = smsText.replace(",", "");

        String[] AMOUNT_KEYWORDS = {"$", "pesos"};
        Pattern AMOUNT_PATTERN = Pattern.compile("\\d+(\\.\\d+)?");

        Double closestAmount = null;

        // 最近距离
        int minDistance = Integer.MAX_VALUE;

        Matcher matcher = AMOUNT_PATTERN.matcher(smsText);

        while (matcher.find()) {

            String numberString = matcher.group();
            int numberDistance = matcher.start();

            // 先校验该数据是否为目标值
            if (!isAmount(smsText, numberString)) {
                continue;
            }

            double currentAmount = Double.parseDouble(numberString.replace(",", ""));

            if (currentAmount >= 100 && currentAmount <= 100000) {

                int keywordDistance = Integer.MAX_VALUE;

                for (String keyword : AMOUNT_KEYWORDS) {

                    int distance = getDistance(smsText, keyword);

                    if (distance != -1) {
                        keywordDistance = Math.min(keywordDistance, Math.abs(numberDistance - distance));
                    }
                }


                if (keywordDistance <= 10) {

                    if (closestAmount == null) {

                        closestAmount = currentAmount;

                        minDistance = keywordDistance;

                    } else if (currentAmount < closestAmount) {
                        // 找最近的
                        if (keywordDistance < minDistance) {

                            closestAmount = currentAmount;

                            minDistance = keywordDistance;
                        }
                    }

                }
            }
        }
        return closestAmount;
    }



    /**
     * @Author shen
     * @Description 校验数据是否正确
     * @Date 18:45 2023/4/24
     * @Param
     * @return
     **/
    private static boolean isAmount(String smsText, String number) {

        int lastIndex = 0;
        while (lastIndex != -1) {
            lastIndex = smsText.indexOf(number, lastIndex);

            if (lastIndex != -1) {

                if (lastIndex > 0 && !StringUtil.isEmptyOrNvl(smsText.substring(lastIndex - 1, lastIndex))) {

                    String frontStr = smsText.substring(lastIndex - 1, lastIndex);

                    if (!frontStr.equals("$") && (!Character.isLetter(frontStr.charAt(0)) && !frontStr.equals(",") && !frontStr.equals("."))) {
                        lastIndex ++;
                        continue;
                    }

                }

                if (lastIndex < smsText.length() - number.length() && !StringUtil.isEmptyOrNvl(smsText.substring(lastIndex + number.length(), lastIndex + 1 + number.length()))) {

                    String laterStr = smsText.substring(lastIndex + number.length(), lastIndex + 1 + number.length());

                    if (!Character.isLetter(laterStr.charAt(0)) && !laterStr.equals(",") && !laterStr.equals(".")) {

                        lastIndex ++;
                        continue;

                    }

                }

                return true;

            }

            break;
        }

        return false;
    }



    /**
     * @Author shen
     * @Description 拿到位置
     * @Date 17:48 2023/4/23
     * @Param [smsText, keyword]
     * @return int
     **/
    private static int getDistance(String smsText, String keyword) {

        int distance = -1;

        int lastIndex = 0;

        while (lastIndex != -1) {

            lastIndex = smsText.indexOf(keyword, lastIndex);

            if (lastIndex != -1) {

                if (lastIndex > 0 && !StringUtil.isEmptyOrNvl(smsText.substring(lastIndex - 1, lastIndex))) {

                    String frontStr = smsText.substring(lastIndex - 1, lastIndex);

                    if (!frontStr.equals("$") && (!Character.isDigit(frontStr.charAt(0)) && !frontStr.equals(",") && !frontStr.equals("."))) {
//                    if (!Character.isDigit(frontStr.charAt(0)) && !frontStr.matches("\\p{Punct}")) {

                        lastIndex ++;

                        // 表示不满足条件，直接跳过
                        continue;

                    }

                }

                if (lastIndex < smsText.length() - keyword.length() && !StringUtil.isEmptyOrNvl(smsText.substring(lastIndex + keyword.length(), lastIndex + 1 + keyword.length()))) {

                    String laterStr = smsText.substring(lastIndex + keyword.length(), lastIndex + 1 + keyword.length());

                    if (!Character.isDigit(laterStr.charAt(0)) && !laterStr.equals(",") && !laterStr.equals(".")) {
//                        if (!Character.isDigit(laterStr.charAt(0)) && !laterStr.matches("\\p{Punct}")) {

                        lastIndex ++;

                        // 表示不满足条件，直接跳过
                        continue;

                    }

                }


                // 拿到符合条件的数据了
                distance = lastIndex;

            }

            break;

        }


        return distance;

    }


}
